package top.malaoshi.ctrl;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import top.malaoshi.model.Result;
import top.malaoshi.service.LoginSrvImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
//@CrossOrigin(allowCredentials="true")
public class LoginCtrl {
    @Resource
    private LoginSrvImpl srv;
    @RequestMapping("/doLogin")
    public Result doLogin(String username, String password, HttpServletRequest request){
        HttpSession session=request.getSession();
        System.out.println("sessionid"+session.getId());
        String userinfo=(String)session.getAttribute("userinfo");
        System.out.println("userinfo:"+userinfo);

        if(userinfo==null||"".equals(userinfo)){
            System.out.println("向session放入数据");
            session.setAttribute("userinfo","phone");
        }
        System.out.println(username+"---"+password);


     Result res=new Result();
       if(username==null||"".equals(username)){
           res.setCode(0);
           res.setMsg("用户名不能为空");
            return res;
        }else if(password==null||"".equals(password)){
            res.setCode(1);
            res.setMsg("密码不能为空");
            return res;
        }else {
           res = srv.login(username, password);
           return res;
       }

    }
}
